package com.wenjh.jdmzt.android.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wenjh.jdmzt.R;
import com.wenjh.jdmzt.util.StringUtils;

/**
 * 自定义的软件标题栏
 *
 * @author jianhua
 *
 */
public class ActionBar extends LinearLayout /*implements ViewFactory*/ {
	private final static int LAYOUT = R.layout.common_actionbar;

	/** 图标容器。显示在Header的左边，其内部已经默认包含一个Logo图标。这个栏目默认是不显示的。 */
	private View iconContainer = null;
	/** 按钮容器。靠右显示。 */
	private LinearLayout buttonContainer = null;
	/** 放置右边View的容器。 */
	private LinearLayout rightViewContainer = null;
	private View middleContainer = null;

	/** 放置左边View的容器。 */
	private LinearLayout leftViewContainer = null;

	/** 用户的头像位。也可以放置其它图标。显示在 iconContainerView 的右边。默认不显示 */
	private ImageView avatarView = null;
	/** 显示软件的LOGO图标 */
	private ImageView logoView = null;
	/** iconContainer 中的一个图标。默认显示LOGO图标。 */
	private ImageView iconView = null;

	/** 标题。靠左显示。加粗并且支持自动滚动。 */
	private TextView titleView = null;
	private TextView subTitleView = null;

	private CharSequence titleText = "";

	private Animation buttonInAnimation, buttonOutAnimation;

	private boolean buttonIsShown = true;

	public enum HeaderType {
		ONLY_SHOWLEFT, ONLY_SHOWRIGHT, SHOW_LEFTRIGHT, HIDE_BUTTON
	}

	public ActionBar(Context paramContext) {
		super(paramContext);
		inflate();
	}

	public ActionBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		inflate();
		
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.ActionBar);
		String title = a.getString(R.styleable.ActionBar_android_text);
		
		if(!StringUtils.isEmpty(title)) {
			setTitleText(title);
		}
	}

	/**
	 * 初始化界面
	 */
	private void inflate() {
		LayoutInflater inflater = ((LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
		inflater.inflate(LAYOUT, this, true);

		iconContainer = findViewById(R.id.header_layout_icon_container);
		buttonContainer = (LinearLayout) findViewById(R.id.header_layout_button_container);
		rightViewContainer = (LinearLayout) findViewById(R.id.header_layout_rightview_container);
		leftViewContainer = (LinearLayout) findViewById(R.id.header_layout_leftview_container);
		middleContainer = findViewById(R.id.header_layout_middle);
		
		avatarView = (ImageView) findViewById(R.id.header_iv_avatar);
		logoView = (ImageView) findViewById(R.id.header_iv_logo);
		titleView = (TextView) findViewById(R.id.header_stv_title);
		subTitleView = (TextView) findViewById(R.id.header_tv_subtitle);
		iconView = (ImageView) findViewById(R.id.header_iv_icon);
	}

	/**
	 * LeftIcon 是一个显示在Header最左边的图标位。 传入一个小于或等于0的数，表示不显示此控件。
	 *
	 * @param resId
	 */
	public void setLeftIcon(int resId) {
		if (resId <= 0) {
			iconContainer.setVisibility(View.GONE);
			return;
		}
		iconContainer.setVisibility(View.VISIBLE);
		iconView.setImageResource(resId);
	}

	/**
	 * LeftIcon 的点击事件。
	 *
	 * @param clickListener
	 */
	public void setLeftIconViewOnClickListener(
			View.OnClickListener clickListener) {
		iconView.setOnClickListener(clickListener);
	}

	/**
	 * 设置头像位的显示内容。头像位显示在Logo的左边。 传入一个小于或等于0的数，表示不显示此控件。
	 *
	 * @param resId
	 */
	public void setAvatar(int resId) {
		if (resId <= 0) {
			avatarView.setVisibility(View.GONE);
			return;
		}
		avatarView.setImageResource(resId);
		avatarView.setVisibility(View.VISIBLE);
	}

	/**
	 * 设置Logo是否显示。
	 *
	 * @param
	 */
	public void setLogoVisibility(boolean visible) {
		if (visible)
			logoView.setVisibility(View.VISIBLE);
		else
			logoView.setVisibility(View.GONE);
	}

	/**
	 * 设置 LogoView 的点击事件监听
	 *
	 * @param clickListener
	 */
	public void setLogoViewOnClickListener(View.OnClickListener clickListener) {
		logoView.setOnClickListener(clickListener);
	}

	/**
	 * 标题的点击事件
	 *
	 * @param listener
	 */
	public void setTitleViewOnClickListener(View.OnClickListener listener) {
		this.titleView.setOnClickListener(listener);
	}

	public void setTitleText(CharSequence text) {
		this.titleText = text;
		this.titleView.setText(this.titleText);

		if (text.toString().trim().length() > 0)
			titleView.setVisibility(View.VISIBLE);
		else
			titleView.setVisibility(View.INVISIBLE);
	}
	
	
	public void setSubTitleText(CharSequence text) {
		this.subTitleView.setText(text);

		if (text.toString().trim().length() > 0)
			subTitleView.setVisibility(View.VISIBLE);
		else
			subTitleView.setVisibility(View.GONE);
	}

	public CharSequence getTitleText() {
		return titleText;
	}
	
	/**
	 * 根据字符串id，显示标题文字
	 *
	 * @param resId
	 */
	public void setTitleText(int resId) {
		String str = this.getResources().getString(resId);
		setTitleText(str);
	}
	
	public ImageView getLeftLogoView() {
		return (ImageView) findViewById(R.id.header_iv_logo);
	}

	/**
	 * 添加一个按钮
	 *
	 * @param v
	 */
	public void addButton(ActionButton button, View.OnClickListener l) {
		if (button == null)
			return;

		addButton(button, l, 0);
	}

	/**
	 * 添加一个按钮到指定位置
	 *
	 * @param v
	 * @param index
	 */
	public void addButton(ActionButton button, View.OnClickListener l, int index) {
		if (button == null)
			return;

		if (l != null) {
			button.setOnClickListener(l);
		}

		buttonContainer.setVisibility(View.VISIBLE);
		buttonContainer.addView(button, index);
	}


	/**
	 * 移除按钮
	 */
	public void removeButton(ActionButton button) {
		buttonContainer.removeView(button);

		if (buttonContainer.getChildCount() == 0) {
			buttonContainer.setVisibility(View.GONE);
		}
	}
	/**
	 * 添加一个放置在Header右边的View
	 */
	public void addRightView(View v) {
		if (v == null)
			return;

		rightViewContainer.addView(v);
		rightViewContainer.setVisibility(View.VISIBLE);
	}

	/**
	 * 添加一个在靠左显示，但是在logo图标右边的控件
	 *
	 * @param v
	 */
	public void addLeftView(View v) {
		if (v == null)
			return;

		leftViewContainer.addView(v);
		leftViewContainer.setVisibility(View.VISIBLE);
	}

	/**
	 * 移除图标
	 *
	 * @param v
	 */
	public void removeRightView(View v) {
		rightViewContainer.removeView(v);
	}

	public void setLogoView(int resId){
		this.logoView.setImageResource(resId);
	}
	
	public void setLogoView(Bitmap bitmap){
		this.logoView.setImageBitmap(bitmap);
	}
	
}
